package javolution.context;

import javolution.util.FastMap;
import javolution.util.FastTable;

/* loaded from: classes2.dex */
public class HeapContext extends AllocatorContext {
    private static final ThreadLocal FACTORY_TO_ALLOCATOR = new ThreadLocal() { // from class: javolution.context.HeapContext.1
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new FastMap();
        }
    };
    private static final ThreadLocal ACTIVE_ALLOCATORS = new ThreadLocal() { // from class: javolution.context.HeapContext.2
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new FastTable();
        }
    };
    private static final Class CLASS = new HeapContext().getClass();

    /* loaded from: classes2.dex */
    private static final class HeapAllocator extends Allocator {
        private final ObjectFactory _factory;

        public HeapAllocator(ObjectFactory objectFactory) {
            this._factory = objectFactory;
        }

        @Override // javolution.context.Allocator
        protected Object allocate() {
            return this._factory.create();
        }

        /* JADX INFO: Access modifiers changed from: protected */
        /* JADX WARN: Multi-variable type inference failed */
        @Override // javolution.context.Allocator
        public void recycle(Object obj) {
            if (this._factory.doCleanup()) {
                this._factory.cleanup(obj);
            }
            if (this.queueSize >= this.queue.length) {
                resize();
            }
            T[] tArr = this.queue;
            int i = this.queueSize;
            this.queueSize = i + 1;
            tArr[i] = obj;
        }

        public String toString() {
            return "Heap allocator for " + this._factory.getClass();
        }
    }

    public static HeapContext enter() {
        return (HeapContext) Context.enter(CLASS);
    }

    public static HeapContext exit() {
        return (HeapContext) Context.exit();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // javolution.context.AllocatorContext
    public void deactivate() {
        FastTable fastTable = (FastTable) ACTIVE_ALLOCATORS.get();
        int size = fastTable.size();
        for (int i = 0; i < size; i++) {
            ((Allocator) fastTable.get(i)).user = null;
        }
        fastTable.clear();
    }

    @Override // javolution.context.Context
    protected void enterAction() {
        getOuter().getAllocatorContext().deactivate();
    }

    @Override // javolution.context.Context
    protected void exitAction() {
        deactivate();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // javolution.context.AllocatorContext
    public Allocator getAllocator(ObjectFactory objectFactory) {
        FastMap fastMap = (FastMap) FACTORY_TO_ALLOCATOR.get();
        HeapAllocator heapAllocator = (HeapAllocator) fastMap.get(objectFactory);
        if (heapAllocator == null) {
            heapAllocator = new HeapAllocator(objectFactory);
            fastMap.put(objectFactory, heapAllocator);
        }
        if (heapAllocator.user == null) {
            heapAllocator.user = Thread.currentThread();
            ((FastTable) ACTIVE_ALLOCATORS.get()).add(heapAllocator);
        }
        return heapAllocator;
    }
}
